/*
package main

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var db *gorm.DB

func init() {
	dsn := "root:root123@tcp(127.0.0.1:3307)/mydb?charset=utf8mb4&parseTime=True&loc=Local"
	d, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("failed to connect database")
	}
	db = d
}

// 定义结构体，包含姓名、班级、学号字段
type Student struct {
	gorm.Model
	Name     string
	Class    string
	StudentID uint
}

// 创建表
func create(db *gorm.DB) {
	db.AutoMigrate(&Student{})
}

// 插入数据
func insert(db *gorm.DB) {
	s := Student{
		Name:     "周莹婕",
		Class:    "区块链2班",
		StudentID: 2023140041771,
	}
	db.Create(&s)
}

// 查询
func find(db *gorm.DB) {
	var student Student
	db.First(&student, 1) // 根据整形主键查找
	fmt.Printf("student: %v\n", student)
	db.First(&student, "name = ?", "周莹婕") // 查找 name 字段值为 周莹婕 的记录
	fmt.Printf("student: %v\n", student)
}

// 更新
func update(db *gorm.DB) {
	var student Student
	db.First(&student, 1) // 根据整形主键查找
	db.Model(&student).Update("StudentID", 2023140041772)
	db.Model(&student).Updates(Student{StudentID: 2023140041772, Name: "周莹婕111", Class: "区块链2班"}) // 仅更新非零值字段
	db.Model(&student).Updates(map[string]interface{}{"StudentID": 2023140041772, "Name": "周莹婕222", "Class": "区块链2班"})
}

// 删除
func delete(db *gorm.DB) {
	var student Student
	db.First(&student, 2) // 根据整形主键查找
	db.Delete(&student, 2)
}

func main() {
	//create(db)
	//insert(db)
	find(db)
	//update(db)
	//delete(db)
}
*/